package licode.unisop.authcenter.adapter.web;

import licode.unisop.authcenter.application.executor.CenterRealmUserQueryExecutor;
import licode.unisop.authcenter.application.info.RealmUserGroupOfPlatPageIn;
import licode.unisop.authcenter.application.info.RealmUserGroupQueryPageIn;
import licode.unisop.authcenter.application.info.RealmUserQueryPageIn;
import licode.unisop.authcenter.application.info.UserLookupIn;
import licode.unisop.client.inter.HasAuthority;
import licode.unisop.provider.info.SiRealmAssignPlatPageIn;
import licode.unisop.provider.info.SiRealmCanAssignPlatPageIn;
import licode.unisop.provider.info.SiRealmFind;
import licode.unisop.provider.info.UserIdInfo;
import licode.unisop.provider.vo.*;
import licode.unisop.types.AuthConst;
import licode.unisop.unilite.result.UniReturnResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 定义统一认证平台中心领域查询相关接口
 *
 * @author licode
 */
@Slf4j
@UniReturnResult
@RestController
@RequestMapping("/uni-center/v1/realm/user")
public class CenterRealmUserQueryController {
    @Resource
    private CenterRealmUserQueryExecutor centerRealmUserQueryExecutor;

    /**
     * 分页查询领域用户信息
     */
    @RequestMapping(value = "/query/page", method = RequestMethod.POST)
    @HasAuthority(roles= AuthConst.SYSTEM_ADMIN, permits = "realm:query")
    public SiPageResult<SiUser> queryPage(@RequestBody RealmUserQueryPageIn req) {
        return centerRealmUserQueryExecutor.queryPage(req);
    }

    /**
     * 分页查询领域分组分配的平台
     */
    @RequestMapping(value = "/group/query/assign-platform/page", method = RequestMethod.POST)
    @HasAuthority(roles=AuthConst.SYSTEM_ADMIN, permits = "realm:query")
    public SiPageResult<SiPlatform> queryAssignPlatformPage(@RequestBody SiRealmAssignPlatPageIn req) {
        return centerRealmUserQueryExecutor.queryAssignPlatformPage(req);
    }

    /**
     * 分页查询能够分配给领域分组的平台列表
     */
    @RequestMapping(value = "/group/query/can-assign-platforms/page", method = RequestMethod.POST)
    @HasAuthority(roles=AuthConst.SYSTEM_ADMIN, permits = "realm:query")
    public SiPageResult<SiPlatform> queryCanAssignPlatformPage(@RequestBody SiRealmCanAssignPlatPageIn req) {
        return centerRealmUserQueryExecutor.queryCanAssignPlatformPage(req);
    }

    /**
     * 查询指定领域用户信息
     */
    @RequestMapping(value = "/query/lookup", method = RequestMethod.POST)
    @HasAuthority(roles=AuthConst.SYSTEM_ADMIN, permits = "realm:query")
    public SiUser lookup(@RequestBody UserLookupIn req) {
        return centerRealmUserQueryExecutor.lookup(req);
    }

    /**
     * 按层次分页读取领域用户的分组，根据传入的平台ID过滤掉已经加入平台的分组
     * 此方法会根据parentId查询其子分组信息
     */
    @RequestMapping(value = "/group/level/page", method = RequestMethod.POST)
    @HasAuthority(roles=AuthConst.SYSTEM_ADMIN, permits = "realm:query")
    public SiUser queryUserGroupLevel(@RequestBody RealmUserGroupQueryPageIn req) {
        return centerRealmUserQueryExecutor.queryUserGroupLevel(req);
    }

    /**
     * 查询分配给平台的领域用户分组
     */
    @RequestMapping(value = "/group/of-platform/list", method = RequestMethod.POST)
    @HasAuthority(roles=AuthConst.SYSTEM_ADMIN, permits = "realm:query")
    public List<SiUserGroup> queryGroupOfPlatformList(@RequestBody RealmUserGroupOfPlatPageIn req) {
        return centerRealmUserQueryExecutor.queryGroupOfPlatformList(req);
    }

    /**
     * 查询领域用户分组列表信息
     */
    @RequestMapping(value = "/group/of-realm/list", method = RequestMethod.POST)
    @HasAuthority(roles=AuthConst.SYSTEM_ADMIN, permits = "realm:query")
    public List<SiUserGroup> queryGroupOfRealmList(@RequestBody SiRealmFind req) {
        return centerRealmUserQueryExecutor.queryGroupOfRealmList(req);
    }

    /**
     * 根据领域和用户查找用户的账号信息
     * @param idFind 查找条件
     * @return 用户账号信息
     */
    @RequestMapping(value = "/account/lookup", method = RequestMethod.POST)
    @HasAuthority(roles=AuthConst.SYSTEM_ADMIN, permits = "realm:query")
    public SiAccountSet findAccount(@RequestBody UserIdInfo idFind) {
        return centerRealmUserQueryExecutor.findAccount(idFind);
    }
}
